<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/11/16
 * Time: 15:58
 */

namespace app\api\controller;


use app\common\controller\Api;
use think\Db;

class Collage extends Api
{
    protected  $noNeedRight = ['*'];
    protected  $noNeedLogin = ['*'];

    //拼团活动列表
    public function collageList(){
        $data['list'] = Db::name('goods_collage')->select();
        if (!empty($data)){
            $this->error('无数据');
        }
        foreach ($data['list'] as $k=>$v){
            $data['list'][$k]['goods_info'] = Db::name('goods_spu')->where('id',$v['goods_id'])->find();
        }
        $this->success('请求成功',$data);
    }

    //拼团商品详情
    public function collageInfo(){
        $collage_id   = (int)input('post.collage_id');
        $goods_id   = (int)input('post.goods_id');
        if (!$collage_id){
            $this->error('collage_id不能为空');
        }
        if (!$goods_id){
            $this->error('goods_id不能为空');
        }
        $data['goods'] = Db::name('goods_spu')->where(['id'=>$goods_id])->find();
        if (!empty($data)){
            $this->error('无数据');
        }
        $url = 'https://'.$_SERVER['HTTP_HOST'];
        $data['goods']['images'] = explode(',',$data['goods']['images']);
        for ($i = 0 ;$i < count($data['goods']['images']);$i++){
            $data['goods']['images'][$i] = $url.$data['goods']['images'][$i];
        }
        $data['spec']  = Db::name('goods_spec')->where(['goods_id'=>$goods_id])->select();
        foreach ($data['spec'] as $k=>$v){
            $name = Db::name('spec')->where('id',$v['spec_id'])->find();
            $data['spec'][$k]['spec_id'] = $name;
            $data['spec'][$k]['image'] = $url.$data['spec'][$k]['image'];
        }
        $data['commentNumber'] = Db::name('comment')->where(['goods_id'=>$goods_id])->count();
        $data['collage'] = Db::name('goods_collage')->where('id',$collage_id)->where('status',1)->find();
        $this->success('请求成功',$data);
    }

    //下单页面展示
    public function collageNow(){
        $user_id = (int)input('post.user_id');
        $goods_id   = (int)input('post.goods_id');
        $number   = (int)input('post.number');
        $spec     = (int)input('post.spec_id');
        $collage_id  = (int)input('post.collage_id');
        if (!$goods_id){
            $this->error('参数goods_id不能为空');
        }
        if (!$user_id){
            $this->error('参数user_id不能为空');
        }
        if (!$number){
            $this->error('参数number不能为空');
        }
        $goods_collage = Db::name('goods_collage')->where('id',$collage_id)->find();
        if (!empty($goods_collage)){
            $this->error('活动不存在');
        }
        $ret = Db::name('member_user')->where('id',$user_id)->find();
        $data['address'] = $ret['addr_id'] ? Db::name('address')->where('id',$ret['addr_id'])->find() : null;
        $data['goods']   = Db::name('goods_spu')->where('id',$goods_id)->find();
        if ($spec){
            $data['spec'] = Db::name('goods_spec')->where('id',$spec)->find();
        }else{
            $data['spec'] = null;
        }
        $data['number'] = $number;
        $data['total'] = $data['goods']['price'] * $number;
        if (!$data){
            $this->error('无数据');
        }
        $this->success('请求成功',$data);
    }

    //发起拼团
    public function collageStar(){
        $user_id     = (int)input('post.user_id');
        $goods_id    = (int)input('post.goods_id');
        $number      = (int)input('post.number');
        $spec        = (int)input('post.spec_id');
        $address     = (int)input('post.addr_id');
        $collage_id  = (int)input('post.collage_id');
        $discount_id = (int)input('post.discount_id');
        $remark      = trim(input('post.remark'));
        if (!$goods_id){
            $this->error('参数goods_id不能为空');
        }
        if (!$user_id){
            $this->error('参数user_id不能为空');
        }
        if (!$number){
            $this->error('参数number不能为空');
        }
        if (!$address){
            $this->error('参数addr_id不能为空');
        }
        $goods_collage = Db::name('goods_collage')->where('id',$collage_id)->find();
        if (!empty($goods_collage)){
            $this->error('活动不存在');
        }
        $goods = Db::name('goods_spu')->where('id',$goods_id)->find();
        $price = $goods['price'];
        if ($spec){
            $ret = Db::name('goods_spec')->where($spec)->find();
            $price = $ret['price'];
            if ($ret['stock'] < $number){
                $this->error('库存不足');
            }
        }
        if ($goods['stock'] < $number){
            $this->error('库存不足');
        }
        $orderMoney = $price * $number;
        $payMoney   = $orderMoney;
        if ($discount_id){
            $ret = Db::name('discount')->where('id',$discount_id)->find();
            $payMoney = $orderMoney - $ret['money'];
        }
        if ($goods['ship'] == 1){
            $payMoney = $payMoney - $goods['ship_money'];
        }
        $order_id = date('Ymdhis') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);;
        $data = [
            'order_id'=>$order_id,
            'user_id' =>$user_id,
            'money'   =>$orderMoney,
            'pay_money'=>$payMoney,
            'pay_status'=>0,
            'deliver_type'=>0,
            'status'=>0,
            'addr_id'=>$address,
            'time'=>time(),
            'collage'=>1,
            'remark' =>$remark
        ];
        $order = Db::name('order_sku')->insert($data);
        $goods_data = [
            'order_id'=>$data['order_id'],
            'goods_id'=>$goods['id'],
            'price'   =>$price,
            'spec_id' =>$spec,
            'number'  =>$number
        ];
        $order_goods = Db::name('order_goods')->insert($goods_data);
        $collageOrder_id = date('Ymdhis') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);;
        $endtime = strtotime('+'.$goods_collage['effecttime'].'hour');
        $data1 = [
            'user_id'=>$user_id,
            'collageOrder_id'=>$collageOrder_id,
            'collage_id'=>$collage_id,
            'order_id'  =>$order_id,
            'createtime'=>time(),
            'endtime'   =>$endtime,
            'effecttime'=>$goods_collage['effecttime'],
            'people'    =>$goods_collage['people'],
            'speople'   =>1,
            'status'    =>0
        ];
        $collage = Db::name('collage_group')->insert($data1);
        $data2 = [
            'user_id'=>$user_id,
            'collage_id'=>$data1['collageOrder_id'],
            'order_id' =>$order_id,
            'time'=>time()
        ];
        $people = Db::name('collage_people')->insert($data2);
        if ($order && $collage && $order_goods){
            $this->success('请求成功',$data);
        }else{
            $this->error('请求失败');
        }
    }

    //参团
    public function collageJoin(){
        $user_id     = (int)input('post.user_id');
        $goods_id    = (int)input('post.goods_id');
        $number      = (int)input('post.number');
        $spec        = (int)input('post.spec_id');
        $address     = (int)input('post.addr_id');
        $collage_id  = (int)input('post.collage_id');
        $head_id     = (int)input('post.head_id');
        $discount_id = (int)input('post.discount_id');
        $remark      = trim(input('post.remark'));
        $goods_collage = Db::name('goods_collage')->where('id',$collage_id)->find();
        if (!empty($goods_collage)){
            $this->error('活动不存在');
        }
        $collage = Db::name('collage_group')->where('collage_id',$collage_id)->where('user_id',$head_id)->find();
        if ($collage['people'] == $collage['speople']){
            $this->error('参团人数已满');
        }
        $goods = Db::name('goods_spu')->where('id',$goods_id)->find();
        $price = $goods['price'];
        if ($spec){
            $ret = Db::name('goods_spec')->where($spec)->find();
            $price = $ret['price'];
            if ($ret['stock'] < $number){
                $this->error('库存不足');
            }
        }
        if ($goods['stock'] < $number){
            $this->error('库存不足');
        }
        $orderMoney = $price * $number;
        $payMoney   = $orderMoney;
        if ($discount_id){
            $ret = Db::name('discount')->where('id',$discount_id)->find();
            $payMoney = $orderMoney - $ret['money'];
        }
        if ($goods['ship'] == 1){
            $payMoney = $payMoney - $goods['ship_money'];
        }
        $order_id = date('Ymdhis') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);;
        $data = [
            'order_id'=>$order_id,
            'user_id' =>$user_id,
            'money'   =>$orderMoney,
            'pay_money'=>$payMoney,
            'pay_status'=>0,
            'deliver_type'=>0,
            'status'=>0,
            'addr_id'=>$address,
            'time'=>time(),
            'collage'=>1,
            'remark' =>$remark
        ];
        $order = Db::name('order_sku')->insert($data);
        $updata = [
            'speople'=>$collage['speople'] + 1
        ];
        $collage = Db::name('collage_group')->where('id',$collage['id'])->update($updata);
        $data1 = [
            'user_id'=>$user_id,
            'collage_id'=>$collage['collageOrder_id'],
            'order_id' =>$order_id,
            'time'=>time()
        ];
        $people = Db::name('collage_people')->insert($data1);
        if ($order && $collage && $people){
            $this->success('请求成功',$data);
        }else{
            $this->error('请求失败');
        }
    }


    //拼团定单列表
    public function collageOrderList(){
        $user_id = (int)input('post.user_id');
        $data['list'] = Db::name('collage_people')->where('user_id',$user_id)->order('time desc')->select();
        if (!$data['list']){
            $this->error('无数据');
        }
        foreach ($data['list'] as $k=>$v){
            $data['list'][$k]['collage'] = Db::name('collage_group')->where('collage_id',$v['collage_id'])->find();
            $ret = Db::name('order_goods')->where('order_id',$data['list'][$k]['collage']['order_id'])->find();
            $data['list'][$k]['goods'] = Db::name('goods_spu')->where('id',$ret['goods_id'])->find();
            $data['list'][$k]['goods']['image'] = $this->url.$data['list'][$k]['goods']['image'];
        }
        $this->success('请求成功',$data);
    }


    //拼团定单详情
    public function collageOrderInfo(){
        $collage_id =input('post.collage_id');
        if (!$collage_id){
            $this->error('collage_id不能为空');
        }
        $data['collage'] = Db::name('collage_group')->where('collage_id',$collage_id)->find();
        $ret = Db::name('order_goods')->where('order_id',$data['collage']['order_id'])->find();
        $data['goods'] = Db::name('goods_spu')->where('id',$ret['goods_id'])->find();
        $data['goods']['image'] = $this->url.$data['goods']['image'];
        if ($ret['spec_id']){
            $data['spec'] = Db::name('goods_spec')->where('id',$ret['spec_id'])->find();
            $data['spec']['image'] = $this->url.$data['spec']['image'];
        }else{
            $data['spec'] = null;
        }
        $this->success('请求成功',$data);
    }





}